using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using SQLDAL;
using System.Data.SqlClient;
using System.Collections.Generic;

/// <summary>
/// Summary description for TypeOfFoodBll
/// By: PHUONG
/// Date: 7/5/2009
/// </summary>
public class TypeOfFoodBll
{

	public TypeOfFoodBll()
	{
		//
		// TODO: Add constructor logic here
		//
    }

    #region List all type of food

    public SqlDataReader ListAll()
    {
        return SQLHelper.ExecuteReader(SQLHelper.CONN_STRING, CommandType.StoredProcedure,
                                        "sp_TypeOfFood_List", null);
    }
    #endregion

    #region Create a new type of food

    public int CreateTypeOfFood(TypeOfFoodInfo typeInfo)
    {
        SqlParameter[] parms = { 
            new SqlParameter("@typeID", SqlDbType.Int, 4),
            new SqlParameter("@typeName", SqlDbType.VarChar, 50)
        };
        parms[0].Value = typeInfo.TypeID;
        parms[1].Value = typeInfo.TypeName;

        int result = SQLHelper.ExecuteNonQuery(SQLHelper.CONN_STRING, CommandType.StoredProcedure,
                            "sp_TypeOfFood_Create", parms);
        return result;
    }
    #endregion

    #region Update type of food
    public int UpdateTypeOfFood(TypeOfFoodInfo typeInfo)
    {
        SqlParameter[] parms = { 
            new SqlParameter("@typeID", SqlDbType.Int, 4),
            new SqlParameter("@typeName", SqlDbType.VarChar, 50)
        };
        parms[0].Value = typeInfo.TypeID;
        parms[1].Value = typeInfo.TypeName;

        int result = SQLHelper.ExecuteNonQuery(SQLHelper.CONN_STRING, CommandType.StoredProcedure,
                            "sp_TypeOfFood_Update", parms);
        return result;
    }
    #endregion

    #region Delete type of food
    public int DeleteTypeOfFood(TypeOfFoodInfo typeInfo)
    {
        SqlParameter[] parms = { 
            new SqlParameter("@typeID", SqlDbType.Int, 4),
        };
        parms[0].Value = typeInfo.TypeID;

        int result = SQLHelper.ExecuteNonQuery(SQLHelper.CONN_STRING, CommandType.StoredProcedure,
                            "sp_TypeOfFood_Delete", parms);
        return result;
    }
    #endregion


    /// <summary>
    /// By: PHUONG
    /// Date: 8/5/2009
    /// This function to load all type of food to drop down list 
    /// </summary>
    /// <param name="dropDownList"></param>
  
    #region Load typeofFood to the drop down list

    public void LoadTypeOfFood(DropDownList dropDownList)
    {

        DataSet ds = SQLDAL.SQLHelper.GetDataSetUsingSP(CommandType.StoredProcedure, "sp_TypeOfFood_List", "TypeOfFood");
        dropDownList.DataSource = ds.Tables["TypeOfFood"];
        dropDownList.DataTextField = "typeName";
        dropDownList.DataValueField = "typeID";
        dropDownList.DataBind();
        dropDownList.Items.Insert(0, "---Select Type---");
    }
    #endregion

    #region List typeID by typeName
    public string ListTypeID(TypeOfFoodInfo typeOfFoodInfo)
    {
        SqlParameter[] cmdParms = {
            new SqlParameter("@typeName", SqlDbType.VarChar, 50)
        };
        cmdParms[0].Value = typeOfFoodInfo.TypeName;
        return SQLHelper.ExecuteScalar(SQLHelper.CONN_STRING, CommandType.StoredProcedure,
                                        "sp_TypeOfFood_ListTypeIDByTypeName", cmdParms).ToString();
    }
    #endregion

    
    #region List food by type
    public IList<FoodInfo> ListMenuByTypeID(TypeOfFoodInfo tofInfo)
    {
        IList<FoodInfo>foodInfo = new List<FoodInfo>();
        SqlParameter[] parms = { new SqlParameter("@typeID", SqlDbType.Int)};
        parms[0].Value = tofInfo.TypeID;
        using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING,
            CommandType.StoredProcedure, "sp_Food_ListFoodByType", parms))
        {
            while (reader.Read())
            {
                FoodInfo info = new FoodInfo();
                info.FoodID = reader.GetString(0);
                info.FoodName = reader.GetString(1);
                info.TypeID = reader.GetInt32(2);
                info.Price = reader.GetFloat(3);
                info.FoodPicture = reader.GetString(4);
                info.FoodDescription = reader.GetString(5);

                foodInfo.Add(info);
            }
        }
        return foodInfo;
    }
    #endregion

    #region List all type of food 2
    public IList<TypeOfFoodInfo> GetAll()
    {
        FoodBll foodBll = new FoodBll();
        IList<TypeOfFoodInfo> tofInfo = new List<TypeOfFoodInfo>();
        using (SqlDataReader reader = SQLHelper.ExecuteReader(SQLHelper.CONN_STRING,
            CommandType.StoredProcedure, "sp_TypeOfFood_List", null))
        {
            while (reader.Read())
            {
                TypeOfFoodInfo info = new TypeOfFoodInfo();
                info.TypeID = reader.GetInt32(0);
                info.TypeName = reader.GetString(1);
                info.FoodInfo = ListMenuByTypeID(info);
                tofInfo.Add(info);
            }
        }
        return tofInfo;
    }
    #endregion


}
